Linux Ubuntu is a popular open source operating system that probably needs little introduction. It in fact has become so widely used that Microsoft has even developed a virtual machine to emulate it called WSL that is geared towards software developers. Up until I short while ago I was using this as my environment for writing code with VS Code. This worked fine until a short while ago when I ran into so many issues between this virtual machine and my actual machine that it caused me to migrate my entire environment to Ubuntu. The main two issues being:
- Hot swapping while my React apps were up and running. Many tutorials will say to simply add CHOKIDAR_USEPOLLING=true either in package.json or in a .env file. While this does work you can expect some insane stress on your CPU and memory due to windows watching every single file up until your app for changes whilst simultaneously running your app at start. I for one saw about 50–70% CPU usage and 60–75% memory usage in task manager on average. I currently use an older laptop and couldn’t afford breaking it.
- Port Forwarding from the WSL2 virtual machine to my actual windows machine didn’t ALWAYS match up. This was a nightmare to configure if I was using another app on my computer that needed to connect to a port as well and have it communicate with WSL2. The specific situation I ran into was trying to run a virtual android device using Android Studio and using Expo CLI to run react-native code on it. The instance of Expo CLI running in WSL2 could never find which port the android device was connected to.
Creating An Ubuntu Developer Environment
This is a pretty straight forward process, so much so that I actually created a script called setup-script.sh for a lot of the setup that can be cloned from my github here. Before trying to mess with this script though please complete the 5 steps listed under Instructions For Setup which come before the 6th step involving the script. I use postgresql for a lot of my database development and the setup for this particular step is a bit complicated. You will see on the 8th step of the instructions for the github readme that I reference this blog post for a more thorough explanation on how to setup postgresql. We will cover this in the next section, here we go!
Setting Up Postgresql On Ubuntu
If you’ve followed along with the readme on this script thus far, you are ready to setup postgresql. You will now need to create a SUPERUSER account that reflects your Ubuntu username. open terminal or VS Code and navigate to /etc/postgresql there you will see a folder that is simply a number.
Initially postgresql will not allow you to login as a user other than the default postgres and it won’t even let you login to that if your ubuntu username is not postgres unless you issue a specific command to do so…but who has time for that? Wouldn’t it be easier if you could just type psql and enter the postgresql cli immediately under your username? That’s what we are about to set up.
First you will have to enter the pg_hba.conf file in order to edit permissions in postgresql. In your terminal enter
sudo nano etc/postgresql/12/main/pg_hba.conf
After you enter this and your password you will be presented with a large text file within terminal please scroll down until you see values that look like this:
In the column furthest to the right, we are going to change all of the values temporarily to trust then revert them back to this original state save the the first one. There should be 7 values in total. enter CTRL+X to exit. It will prompt you to save, press y then hit enter.
Now that you are back within terminal, enter the following to start the postgres cli:
sudo -u postgres psql postgres
you will know this is successful because your terminal will start with postgres-#. in order to create a super user enter the following (parts that are relative to your setup will be presented as lower case, please use your ubuntu username as username):
CREATE ROLE username WITH LOGIN SUPERUSER PASSWORD 'password';
after hitting enter you should see a confirmation of CREATE ROLE. Now we are also going to create a database that shares the same name as the superuser you just created. Do so by entering:
CREATE DATABASE username;
after hitting enter you should see a confirmation of CREATE DATABASE. At which point it will be time to revert that pg_hba.conf file back to its original state (except that first column entry). exit the postgresql cli by typing exit and hitting enter. Once in terminal reenter the following:
sudo nano /etc/postgresql/12/main/pg_hba.conf
Inside the text file within terminal please change the column values which at this point should all be trust back to their original state. The end result should resemble:
Once this has been complete press CTRL+X to exit, then y to save, then hit enter. And you’re done! you should now be able to enter postgresql under your username by simply typing psql in the terminal. Don’t forget to exit is simply exit!.
Hopefully this guide has been helpful in setting up a free open source environment for software development. I will most likely be referring back to this if I have to set this up again in the future. Happy coding!