Docker and other containerization tools are great for keeping applications separate and easy to manage. However, sometimes it’s better to run applications directly on your host machine without containers, either because it’s simpler, saves resources, or for specific performance reasons.
This tutorial will help you to install and use multiple .NET core versions on Ubuntu systems. In this guide, we’ll use Ubuntu machine because it’s widely used and works well with the .NET Core.
Prerequisites
- Ubuntu system (20.04 LTS or newer)
- Basic knowledge of using the command line
- Different versions of .NET Core applications (like 3.1 and 6.0)
Step 1: Install .NET SDKs
.NET Core lets you install and use different versions of its software side by side. This allows you to run apps that need different versions of .NET Core on your Ubuntu server.
- Update your packages and install what you need to access Microsoft’s repository:
wget https://packages.microsoft.com/config/ubuntu/$(lsb_release -rs)/packages-microsoft-prod.deb
sudo dpkg -i packages-microsoft-prod.deb
sudo apt update
sudo apt install -y apt-transport-https
sudo apt update
- Install the .NET Core SDKs for the versions you need. For example, to install .NET Core 3.1, 6.0, and 8.0:
sudo apt install -y dotnet-sdk-3.1
sudo apt install -y dotnet-sdk-6.0
sudo apt install -y dotnet-sdk-8.0
Step 2: Configure Each Application
If you’re running more than one app on the same server, you’ll need to set each one to use a different port. This will prevent them from interfering with each other.
- For the first application, change the appsettings.json or appsettings.Production.json file to listen on a specific port, like 5000:
{ "Kestrel": { "Endpoints": { "Http": { "Url": "http://localhost:5000" } } } }
- For the second app, do the same thing, but use a different port, like 5001:
{ "Kestrel": { "Endpoints": { "Http": { "Url": "http://localhost:5001" } } } }
Step 3: Run the Applications
Go to each application’s folder and run them using this command:
dotnet run
Alternatively, if you’re working in a production environment or don’t want to use the dotnet run command, you can publish the apps for deployment:
Use this command to publish your apps, changing the details for your own runtime and paths:
dotnet publish -c Release -r ubuntu.20.04-x64 --output /path/to/YourApp
Run the apps from their published folders.
Step 4: Using a Reverse Proxy (Optional)
For production setups or easier network management, you might want to use a reverse proxy like Nginx or Apache. This will let you direct traffic to the right app based on the URL or domain, even if the apps are using different ports.
Reference:
Step 5: Set Up System Services (Optional)
To make sure your apps run as services and start automatically when the server boots up, you can create systemd service files for each app. Here’s an example for one app:
- Create a service file at /etc/systemd/system/yourapp.service:
[Unit] Description=.NET Web Application [Service] WorkingDirectory=/path/to/app ExecStart=/usr/bin/dotnet /path/to/app/YourApp.dll Restart=always RestartSec=10 SyslogIdentifier=dotnet-yourapp User=www-data Environment=ASPNETCORE_ENVIRONMENT=Production [Install] WantedBy=multi-user.target
- Reload the system daemon:
sudo systemctl daemon-reload
- Enable and start the service:
sudo systemctl enable yourapp.service
sudo systemctl start yourapp.service
- Repeat this for each app, making sure to use different service names and correct paths.
Conclusion
Running multiple .NET Core apps on one Ubuntu server is easy without using Docker or containers. By installing the right SDKs, setting up the apps to use different ports, and optionally using reverse proxies or system services, you can manage and deploy your apps smoothly. This approach gives you flexibility and suits different development and performance needs.