Install and Create Virtualenv Python in ubuntu machine

Let’s say I want to have a project called webproject, and this project has own user with the same name to access the project directory. And this is supposed to be a python project and it needs virtual environment. also I want the project has specific python version 2.7 with virtualenv. So let’s get started.

First you may have to check your python version if it is already installed python 2.7 otherwise you’re gonna have to download and install it, but if you prefer to not use python 2.7, let’s say version 2.6, or whatever. That’s fine as well.

Something related:

Install different version of Python in linux

Let’s start with install the virtualenv via apt in ubuntu

$ apt-get install python-virtualenv virtualenvwrapper

Create user and setup home directory

$ useradd -m webproject -d /srv/webproject

Change user to webproject, until you’re inside the home directory project

$ sudo su - webproject

Create virtual environment with virtualenv with python version 2.7

$ virtualenv ~/venv --python=2.7

Or, if you prefer not to use spesific python version, just run the command like this:

$ virtualenv ~/venv

Now, inside your home directory, there is a new directory named venv that contained your python binary.

You might want to put this line of code inside your .bash_history or .profile inside your home directory, so whenever you try to change user, it is automatically activate your python virtual environment.

$ vim ~/.profile
....
....
source ~/venv/bin/activate

and save the file.

Now, try to change user to webproject:

$ sudo su - webproject
(venv)webproject@localhost:~$

Create simple arg parse in python

argparse is a module to make user-friendly command-line interfaces. It’s probably the one of the most frequently used module when I create a script in python that needs to parse some arguments. Check this out.

$ vim test.py
#!/usr/bin/python
import argparse

def init_args():
  parser = argparse. ArgumentParser(description="This is the description")
  parser.add_argument("--arg1", required=True, type=str, help="This is arg1")
  parser.add_argument("--allow", required=False, action="store_true", help="Allow mode")
  return parser.parse_args()

def main(arg1):
  return "arg1: %s"%(arg1)

if __name__ == "__main__":
  args = init_args()
  main(args.arg1)

See what happens when we run it

$ ./test.py --arg1 "showme"
arg1: showme

Sometimes we want to create an argumen but only store it as a True variable. we can just create simple test like this.

$ vim test.py
#!/usr/bin/python
import argparse

def init_args():
  parser = argparse. ArgumentParser(description="This is the description")
  parser.add_argument("--arg1", required=False, action="store_true", help="Enable arg1")
  return parser.parse_args()

def main():
  return "arg1 is enabled"

if __name__ == "__main__":
  args = init_args()
  if args.arg1:
    main()
  else:
    print "arg1 parse is not enabled"

Try to run it

$ ./test.py --arg1
arg1 is enabled

Setup SSH key passphrase only ask once

Sometimes it’s annoying when you’re trying to work with your project and whenever you need to enter your key passphrase. So I wanna make this passphrase prompt ask only once.

vim ~/.bash_profile

and put this at the bottom of the file :

eval $(ssh-agent)
ssh-add

Or if you don’t want to be asked for passphrase at all, you can just generate new passphrase with no password.


Traceroute command line in ubuntu

This is how to install traceroute in ubuntu machine

apt-get install traceroute

And how to use it :

$ traceroute google.com
traceroute to google.com (74.125.68.100), 30 hops max, 60 byte packets
 1  192.168.100.1 (192.168.100.1)  2.281 ms  2.378 ms  2.398 ms
 2  61.5.80.1 (61.5.80.1)  9.254 ms  9.357 ms  9.378 ms
 3  121.subnet125-160-11.speedy.telkom.net.id (125.160.11.121)  8.980 ms  8.966 ms  8.941 ms
 4  61.94.171.69 (61.94.171.69)  9.611 ms  9.630 ms  9.598 ms
 5  180.240.193.238 (180.240.193.238)  23.731 ms 180.240.193.46 (180.240.193.46)  32.981 ms  33.343 ms
 6  180.240.193.237 (180.240.193.237)  29.036 ms 180.240.193.45 (180.240.193.45)  41.533 ms 180.240.193.237 (180.240.193.237)  24.494 ms
 7  180.240.204.29 (180.240.204.29)  20.850 ms 180.240.204.49 (180.240.204.49)  20.496 ms 180.240.204.29 (180.240.204.29)  20.846 ms
 8  72.14.223.88 (72.14.223.88)  21.115 ms  21.529 ms  24.440 ms

Setup read and write samba share in vagrant ubuntu

I use vagrant for daily development, now vagrant has this directory mounted when we setup vagrant at the first time. But sometimes I’d like to mount my own directory to my local machine.

This is the way I create samba share in my vagrant :

apt-get install samba samba-common

Create user for project

useradd -m user1 -d /srv/myproject

Open samba config file

vim /etc/samba/smb.conf

Put the new config below at the very bottom

[myproject]
browseable = yes
path = /srv/myproject
guest ok = yes
public = yes
read only = no
create mask = 0644
directory mask = 0755
force user = user1

Restart samba :

service samba restart

What’s next :

Mount samba share with command line


Mount samba share with command line

When you work with vagrant, you might want your project folder to be mounted on some directory on your local. You can do that easily with file manager, but here’s my favorite way to mount my samba share in vagrant to my directory on my local. So I can work on that.

Before you do this, you might want to get your current user id, so your project is accessible. And make sure the destination directory is already exists.

sudo mount -t cifs -o uid=1001,gid=1001 //192.168.56.11/kripikpasta /tmp/kripikpasta